package com.kuangchi.test.controller;

import com.alibaba.fastjson.JSON;
import com.kuangchi.test.service.UserService;
import com.kuangchi.test.vo.UserVo;
import com.kuangchi.test.vo.YcWaningFace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import sun.misc.BASE64Decoder;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.List;
import java.util.Map;

/**
 * @version v1.0
 * @类描述：
 * @项目名称：
 * @包名： com.kuangchi.test.web
 * @类名称：UserWeb
 * @创建人：YIHANG.XU
 * @创建时间：2021/12/22:30 下午
 * @修改人：YIHANG.XU
 * @修改时间：2021/12/22:30 下午
 * @修改备注：
 * @Copyright
 */

@RestController
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/addUserList", method = RequestMethod.POST)
    public boolean addUserList(@RequestBody List<UserVo> userVoList) {
        return userService.addUserList(userVoList);

    }


    @RequestMapping(value = "/ocean/auth/token", method = RequestMethod.POST)
    public Root login(@RequestBody Map<String, Object> map) {
        System.out.println(map);
        String s = "{\"code\":\"00000000\",\"message\":\"请求处理成功\",\"data\":{\"token\":\"eyJhbGciOiJIUzUxMiJ9.eyJhcGlLZXkiOiI0RjU3MTQwQzc0QkM0NDFGNjM5NDc1OEE3NkFCRDNBQ0NERjIwOTQ0RjhEQzZDN0I4MkE1RjdGM0YxOUQzM0U2MDNENDQwQ0NDQzNEMkZCNDIxQzk3RUI2RUE0MUQ5M0YiLCJnZW5lcmF0ZVRpbWUiOjE2NDE1MjYxNDkzNjEsImV4cCI6MTY0MTUzMzM0OSwiZGV2aWNlSWQiOiJBUFBfREVWSUNFX0lEIn0.uwUtSaR6wmbEZLCfRRiW-A01J52tkZ-AehK8rFWR-Hh4CDwpWDLZ3je8qgDd40cpN3sBiVvcY7J4O59I3wmVUg\",\"refreshToken\":\"eyJhbGciOiJIUzUxMiJ9.eyJhcGlLZXkiOiI0RjU3MTQwQzc0QkM0NDFGNjM5NDc1OEE3NkFCRDNBQ0NERjIwOTQ0RjhEQzZDN0I4MkE1RjdGM0YxOUQzM0U2MDNENDQwQ0NDQzNEMkZCNDIxQzk3RUI2RUE0MUQ5M0YiLCJzY29wZSI6InJlZnJlc2giLCJnZW5lcmF0ZVRpbWUiOjE2NDE1MjYxNDkzNjEsImV4cCI6MTY0MTUzMzM0OSwiZGV2aWNlSWQiOiJBUFBfREVWSUNFX0lEIn0.7qEv4AGPgwKpj1d3n-2cJZhWATr5d77IH9mdMUrUyfgYvODPUdZJgmZQt4GARWc-a2Rpl5hN8tRlypCPQwTa8w\",\"expires\":7200000},\"error\":[]}";
        Root root = JSON.parseObject(s, Root.class);
        return root;
    }

    @RequestMapping(value = "/ocean/api/recordAlarm/pages", method = RequestMethod.POST)
    public YcWaningFace page(@RequestBody Map<String, Object> map) {
        System.out.println(map);
        String s = "{\"code\":\"00000000\",\"message\":\"请求处理成功\",\"data\":{\"pageSize\":10,\"currentPage\":1,\"totalPages\":1,\"totalRows\":8,\"minRowNumber\":1,\"maxRowNumber\":8,\"datas\":[{\"alarmId\":\"dd900d3dfc394fe59e2d72723a467748\",\"monitorId\":\"GA2011DE0060012021060215093313531\",\"monitorName\":\"测试\",\"monitorType\":1,\"clusterAlarmType\":2,\"monitorProperty\":0,\"topn\":3,\"alarmScore\":0.8045,\"captureId\":\"800041010310000109202201070000000055\",\"captureTime\":1641533154584,\"alarmTime\":1641533154872,\"deviceId\":\"800041010310000109\",\"deviceType\":1,\"deviceName\":\"kc0002\",\"deviceAddress\":\"\",\"devExtField1\":null,\"devExtField2\":null,\"devExtField3\":null,\"monExtField1\":\"1\",\"monExtField2\":null,\"monExtField3\":null,\"lng\":\"106.539859\",\"lat\":\"29.5943\",\"panoramaId\":\"800041010310000109202201070000000054\",\"captureGroup\":null,\"captureUrl\":\"596,559cde645397b4\",\"panoramaGroup\":null,\"panoramaUrl\":\"597,559cdf55c8dfc9\",\"qualityScore\":0.8063,\"age\":null,\"ageGroup\":null,\"gender\":null,\"race\":null,\"faceTotalScore\":0.8063,\"lightScore\":0.6078,\"maskScore\":0.9753,\"maskState\":0,\"clarityScore\":0.8654,\"glassesScore\":0.5138,\"glassesState\":1,\"mouthScore\":0.0030,\"faceClarityScore\":null,\"sunGlassesScore\":0.0006,\"sunGlassesState\":0,\"nation\":null,\"pitch\":-4.2185,\"yaw\":0.5411,\"roll\":-0.0933,\"x\":417.0000,\"y\":304.0000,\"width\":374.0000,\"height\":374.0000,\"printFaceScore\":1.0000,\"fromSystem\":\"41010020215030915041\",\"isLinkage\":null,\"remark\":null,\"createTime\":1641533154872,\"year\":2022,\"month\":202201,\"day\":20220107,\"mon\":1,\"birthday\":null,\"cardNation\":null,\"cardId\":null,\"name\":null,\"cardGender\":null,\"address\":null,\"cardOrg\":null,\"validdateStart\":null,\"validdateEnd\":null,\"score\":null,\"cardGroup\":null,\"cardUrl\":null,\"authResult\":null,\"faceId\":null,\"threshold\":null,\"captureType\":null,\"icCardType\":null,\"icCardNo\":null,\"recordAlarmTopns\":[{\"alarmTopId\":\"627efdfa244248fb9247733431afffb1\",\"alarmId\":\"dd900d3dfc394fe59e2d72723a467748\",\"simScore\":0.9999,\"faceId\":\"700041010310000001202112200000000017\",\"facedbId\":\"700041010310000001\",\"facedbName\":\"测试\",\"faceCardType\":\"515\",\"faceCardId\":\"123\",\"faceName\":\"光启\",\"faceNation\":\"\",\"faceBirthday\":\"20211220\",\"faceAddress\":null,\"faceGender\":1,\"faceCardOrg\":null,\"faceUrlGroup\":null,\"faceUrl\":\"548,4f82071ee0cc25\",\"topSeq\":1,\"fromSystem\":\"41010020215030915041\",\"isLinkage\":null,\"remark\":\"\",\"createTime\":1641533154887,\"year\":2022,\"month\":202201,\"day\":20220107,\"mon\":1,\"faceExtField1\":null,\"faceExtField2\":null,\"faceExtField3\":null,\"facedbExtField1\":null,\"facedbExtField2\":null,\"facedbExtField3\":null,\"faceUrlFull\":\"http://127.0.0.1:8980/getImage\"}],\"minorityState\":0,\"captureUrlFull\":\"http://127.0.0.1:8980/getImage\",\"panoramaUrlFull\":\"http://127.0.0.1:8980/getImage\",\"cardUrlFull\":null}]},\"error\":[]}";
        YcWaningFace YcWaningFace = JSON.parseObject(s, YcWaningFace.class);
        return YcWaningFace;
    }

    @GetMapping(value = "/getImage",produces = MediaType.IMAGE_JPEG_VALUE)

    public byte[] getImage() {
        String path = "/Users/xuyihang/Downloads/test.jpg";
        return image2Bytes(path);

    }

    public static void main(String[] args) throws Exception {
        String base64 = IamgeIdUtil.byte2base64(image2Bytes("/Users/xuyihang/Downloads/李岩专业测试照片.jpg"), "ef5279fcd74e46e28e16e740f99e34fc");
        GenerateImage(base64);
        String path = "/Users/xuyihang/Downloads/test.jpg";

        String s = IamgeIdUtil.byteGetId(image2Bytes(path));
        System.out.println(s.length());
    }
    // 对字节数组字符串进行Base64解码并生成图片
    public static boolean GenerateImage(String imgStr) {
        if (imgStr == null) {
            // 图像数据为空
            return false;
        }
        BASE64Decoder decoder = new BASE64Decoder();
        try {
            // Base64解码
            byte[] b = decoder.decodeBuffer(imgStr);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// 调整异常数据
                    b[i] += 256;
                }
            }
            // 生成jpeg图片
            String imgFilePath = "/Users/xuyihang/Downloads/test.jpg";// 新生成的图片
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    static void ByteToFile(byte[] bytes) throws Exception {

        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        BufferedImage bi1 = ImageIO.read(bais);
        try {
            File w2 = new File("/Users/xuyihang/Downloads/qqqqqq.jpg");//可以是jpg,png,gif格式
            ImageIO.write(bi1, "jpg", w2);//不管输出什么格式图片，此处不需改动
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            bais.close();
        }
    }

    public static byte[] image2Bytes(String imgSrc) {
        FileInputStream fin;
        byte[] bytes = null;
        try {
            fin = new FileInputStream(new File(imgSrc));
            bytes = new byte[fin.available()];
            //将文件内容写入字节数组
            fin.read(bytes);
            fin.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        return bytes;
    }


}
